﻿using sqlsearcher.Models.Services;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace sqlsearcher.Services
{
    public class SqlServerDbUtils: ISqlDbUtils
    {
        public DataSet QueryDs(string db,string sql)
        {
            var ds = new DataSet();
            using (SqlConnection conn = new SqlConnection(SqlServerHelper.BuilderConnectionString(db)))
            {
                try
                {
                    conn.Open();

                    SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                    da.Fill(ds);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }
            return ds;
        }
        

       

        public int Cmd(string db,string sql)
        {
            var result = 0;
            using (SqlConnection conn = new SqlConnection(SqlServerHelper.BuilderConnectionString(db)))
            {
                try
                {
                    conn.Open();

                    SqlCommand cmd = new SqlCommand(sql, conn);
                    result = cmd.ExecuteNonQuery();
                    
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }
            return result;
        }

        public int ExecSPSql(string db,string sql)
        {
            var result = 0;
            Queue que = new Queue();
            string[] sArray = Regex.Split(sql, "GO", RegexOptions.IgnoreCase);
            foreach (var item in sArray)
            {
                if(!string.IsNullOrWhiteSpace(item.Trim()))
                que.Enqueue(item);
            }
            int queCount = que.Count;

            using (SqlConnection conn = new SqlConnection(SqlServerHelper.BuilderConnectionString(db)))
            {
                conn.Open();
                for (int i = 0; i < queCount; i++)
                {
                    try
                    {

                        SqlCommand cmd = new SqlCommand(que.Dequeue().ToString(), conn);
                        int queryCont = cmd.ExecuteNonQuery();
                        if (queCount > 0)
                        {
                            result += queCount;
                        }

                    }
                    catch 
                    {
                        continue;
                    }
                }
               
            }
            return result;
            
        }

        public DataTable Query(string db, string sql)
        {
            var dt = new DataTable();
            using (SqlConnection conn = new SqlConnection(SqlServerHelper.BuilderConnectionString(db)))
            {
                try
                {
                    conn.Open();

                    SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                    da.Fill(dt);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }
            return dt;
        }


        public DataTable Query( string sql)
        {
            var dt = new DataTable();
            using (SqlConnection conn = new SqlConnection(SqlServerHelper.CONNSTR))
            {
                try
                {
                    conn.Open();

                    SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                    da.Fill(dt);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }
            return dt;
        }
        public int ExecSPSql(string sql)
        {
            throw new NotImplementedException();
        }
    }
}
